/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-4-3
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.displaytag.util.ParamEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.UserService;

/**
 * 用户管理控制器
 * 
 * @author springyounger
 */
public class AdminUserController extends MultiActionController {
	private static final Logger logger = LoggerFactory
			.getLogger(AdminUserController.class);
	private UserService userService;

	@SuppressWarnings("unchecked")
	public ModelAndView findByAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.info("用户列表!");
		Map model = new HashMap();
		// 页数的参数名
		String pageIndexName = new ParamEncoder("row")
				.encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);
		// 每页显示的条数
		int pageSize = 10;
		// 当前页
		int pageIndex = LotteryUtil
				.isEmpty(request.getParameter(pageIndexName)) ? 0 : (Integer
				.parseInt(request.getParameter(pageIndexName)) - 1);
		// 总记录
		int resultSize = userService.getAllUserCount();
		// 显示所有
		List<User> userList = userService.getUsers(pageIndex, pageSize);

		model.put("resultSize", resultSize);
		model.put("userList", userList);
		return new ModelAndView("admin/userList", model);
	}

	@SuppressWarnings("unchecked")
	public ModelAndView findByName(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		logger.info("搜索用户{}", username);
		// 根据name模糊查询查找
		Map model = new HashMap();
		// 页数的参数名
		String pageIndexName = new ParamEncoder("row")
				.encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);
		// 每页显示的条数
		int pageSize = 10;
		// 当前页
		int pageIndex = LotteryUtil
				.isEmpty(request.getParameter(pageIndexName)) ? 0 : (Integer
				.parseInt(request.getParameter(pageIndexName)) - 1);
		// 总记录
		int resultSize = userService.getLikeUserCount(username);
		List<User> userList = userService.getLikeUserByName(pageIndex,
				pageSize, username);

		model.put("resultSize", resultSize);
		model.put("userList", userList);
		return new ModelAndView("admin/userList", model);
	}

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		logger.info("删除用户 {}", id);
		if (LotteryUtil.isEmpty(id)) {
			return findByAll(request, response);
		}
		int userid = Integer.parseInt(id);
		userService.deleteUser(userid);
		return findByAll(request, response);
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
